<template>
<view >




 <view class="flex-column-center container">

    <!-- <text class="ft24 dlc-gray mg10-y">申请获取你的公开信息（昵称、头像等）</text> -->


    <!-- <button
      open-type="getUserInfo"
      @getuserinfo="getUserInfoEvent"
      lang="zh_CN"
      type="primary"
      class="authBtn mg50-y"
    >微信授权</button> -->

    <button @click="auth" type="primary" class="mgt300"> 微信用户登录</button>
   
     

    <i-toast id="toast" />


   
  </view>
</view>
</template>
<script>
import store from "@/store";
import { authorization, register } from "@/api";
 import areaList from '@/utils/area';
import { REQUEST_BASE_URL } from "@/config";
const { $Toast } = require("@/wxcomponents/iview/base/index");
export default {
  data() {
    return {
      areaShow:false,
      pca:"",
      areaList:areaList,
      province:"",
      city:"",
      area:"",
      authVisi: false,
      actions: [
        {
          name: "去绑定",
          color: "#2d8cf0"
        }
      ],
      userInfo: {},
      logo: null,
      appName: "",
      isChecked:false
    };
  },
  methods: {
   showAddress(){
     this.areaShow=true
   },
   onSelect(e){
    this.pca= e.detail.values[0].name+"-"+
    e.detail.values[1].name+"-"+e.detail.values[2].name
    this.province=e.detail.values[0].name
    this.city=e.detail.values[1].name
    this.area=e.detail.values[2].name
    this.areaShow=false
   },
   async getUserInfoEvent(e) {
     console.log(e)
          if (e.mp.detail.errMsg === "getUserInfo:ok"){
            //调用服务器接口
          this.userInfo = await e.mp.detail.userInfo;
          console.log(e.mp.detail.userInfo, '11111')
          this.register();

        } else {
          $Toast({
            content: "您拒绝了授权",
            type: "warning",
            duration: 3000
          });
        }
    },
    // 检测权限
    async auth(option) {
      let [a, { code, errMsg }] = await uni.login();
      if (errMsg === "login:ok") {
        //调用服务器接口
        const { data } = await authorization({ code });
        if (data.code === 1) {
          const { Authorization, openId } = data.data;
          if (Authorization) {
            //刷新token,和第一次进来getUserInfo
            store.commit("set_token", Authorization);
            await store.dispatch("getUserInfo");
          } else {
            uni.redirectTo({
              url: `/pagesA/auth/index?openId=${openId}`,
            });
          }
        } else {
          return Promise.reject();
        }
      }
       uni.navigateBack({
           delta: 1
       });
    },


    changeUrl(){
         uni.navigateTo({
           url:"agreement"
         })
       },
    async register(e){
       let params = {
        nickName: this.userInfo.nickName,
        avatarUrl: this.userInfo.avatarUrl,
        openId: this.query.openId,
        city:this.city,
        province:this.province,
        area:this.area
      };
      //调用服务器接口
      const { data } = await register(params);
      if (data.code === 1) {
        store.commit("set_token", data.data);
        $Toast({
          content: "授权成功",
          duration: 1000
        });
        setTimeout(() => {
          uni.switchTab({
           url: '/pages/order/inputCode'
          });
        }, 500);
       
      }
    
    }
   },
  onReady(){
  //  store.dispatch('getSystemParam',1).then(res =>{
  //       if (res.code === 1) {
  //         this.logo = REQUEST_BASE_URL + res.data.parameter
  //         this.$forceUpdate()
  //       }
  //     })
  //  store.dispatch('getSystemParam',2).then(res =>{
  //       if (res.code === 1) {
  //         this.appName = res.data.parameter
  //       }
  //     })
  },
  onLoad(query) {
    this.query = { ...query };
  },
  onUnload(){
 
  }
};
</script>

<style lang="stylus" scoped>
.logo {
  margin: 60upx 0;
  width: 240upx;
  height: 240upx;
  border-radius: 50%;
}
.authBtn {
  width: 90%;
}
.inpBot{
  height 60rpx
  width 500rpx
  margin 10rpx auto
  border:1rpx solid #ccc
  text-align left 
  text-indent 10rpx

  }
.wNote
  width 750rpx
.readInfo
  color #2278bf; 
.authDes
  height 80rpx;
.authSel
   vertical-align middle;
   margin-top -2rpx;
   margin-bottom 1rpx;
</style>
